<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="rname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

<!--        表操作行-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

<!--        行操作栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="rolePermission">授权</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {{#  if(d.ustatus===false){ }}
                <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="ice">冻结</a>
            {{#  } else { }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="ice">解冻</a>
            {{#  } }}
        </script>

<!--        这是一个模板语言用来处理状态-->
        <script type="text/html" id="ustatustemplet">
            {{#  if(d.rstatus===false){ }}
                <button type="button" class="layui-btn layui-btn-xs layui-btn-normal">正常使用</button>
            {{#  } else { }}
                <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled">角色注销</button>
            {{#  } }}
        </script>
    </div>
    <!--添加页面与修改页面功能相似-->
    <div id="saveDiv" class="layui-form layuimini-form layui-hide">
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="rname" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入账号" value="" class="layui-input">
                <tip>填写自己账号的名称。</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <div id="updateDiv" class="layui-form layuimini-form layui-hide" lay-filter="updateform">
        <input type="hidden" name="rid" />
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="rname" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入账号" value="" class="layui-input">
                <tip>填写自己账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateBtn">确认保存</button>
            </div>
        </div>
    </div>


    <div id="rolePermission" class="layui-form layuimini-form layui-hide" lay-filter="rolePermission">
        <input type="hidden" name="rid" />
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div id="test7" class="demo-tree"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <a class="layui-btn layui-btn-lg layui-btn-normal"  lay-submit lay-filter="saveRolePermission">保存</a>
            </div>
        </div>
    </div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','tree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            tree = layui.tree,
            table = layui.table;

        /**
         *  js 声明一个方法
         *
         */
        function createIds(array,rearr=[]){

            if(array.length == 0){
                return rearr;
            }else{
                for(let i = 0 ;i < array.length;i++){
                    rearr.push(array[i].id);
                    if(array[i].children.length > 0){
                        createIds(array[i].children,rearr);
                    }
                }
            }
            return rearr;
        }

        table.render({
            id:"mytable",
            elem: '#currentTableId',
            url: '/rbac/role/page',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'rid', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'rname', title: '名称'}
                ,{field: 'rstatus', title: '状态',templet:'#ustatustemplet'}
                ,{title: '操作', minWidth: 249, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('mytable', {
                url: '/rbac/role/page'

                ,page:{
                    curr:1
                }
                ,where:data.field
             });
            return false;
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                method:"post"
                ,url:"/rbac/role/save"
                ,data: JSON.stringify(data.field)  //向后台传递多组参数
                ,contentType:"application/json;charset=UTF-8"
                ,success:function (d){
                    if(d){
                        // 成功
                        layer.msg("添加成功！！！",{icon:1,time:1000},function (){
                            layer.closeAll();
                            //应该对当前页面里面的表格进行刷 新操作
                            table.reload('mytable', {url: '/rbac/role/page'});
                            $("#saveDiv").addClass("layui-hide");
                        });

                    }else{
                        // 失败
                        layer.msg("添加失败！！！",{icon:2,time:1000},function (){
                            layer.closeAll();
                        });
                    }
                }
            });
            return false;
        });

        //监听修改操作
        form.on('submit(updateBtn)', function (data) {
            let vals = data.field;
            $.ajax({
                method:"put",  //请求方式 get post put delete
                url:"/rbac/role/update",// 请求地址  requesturl
                data:vals,
                success:function (d){
                    if(d){
                        // 成功
                        layer.msg("修改成功！！！",{icon:1,time:1000});
                        //应该对当前页面里面的表格进行刷 新操作
                        table.reload('mytable', {url: '/rbac/role/page'});
                        $("#updteDiv").addClass("layui-hide");
                    }else{
                        // 失败
                        layer.msg("修改失败！！！",{icon:2,time:1000});
                    }
                }
            });
            layer.closeAll();
            return false;
        });

        form.on('submit(saveRolePermission)', function (data) {

            //取到右侧全部数据
            var getData = tree.getChecked('pertree');
            console.log(getData,data);
            // 生成当前角色 的全部权限
            var pids = createIds(getData);

            //将提交数组转换成为字符串
            if(getData.length > 0){
                let ps = "";
                for(var i = 0 ;i < pids.length;i++){
                    ps += ("&pids=" + pids[i]);
                }
                console.log("======>")
                $.ajax({
                    method:"post"
                    ,async:false  // 完成同步操作
                    ,url:"/rbac/permission/saveForPermission/"
                    ,data:"rid=" + data.field.rid + ps
                    ,success:function (d){
                        if(d){
                            // 成功
                            layer.msg("角色授权成功！！！",{icon:1,time:1000},function (){
                                layer.closeAll();
                                //应该对当前页面里面的表格进行刷 新操作
                            });

                        }else{
                            // 失败
                            layer.msg("角色授权失败！！！",{icon:2,time:1000},function (){
                                layer.closeAll();
                            });
                        }
                        $("#rolePermission").addClass("layui-hide");
                    }
                });
            }else{
                layer.msg("此项操作将会使用当前用户没有办法登陆系统，请保留一个角色，如果不想使用当前用户，请直接冻结!!"
                    ,{icon:2,time:5,anmi:6})
            }
            return false;
        });
        /**
         * toolbar监听事件 表操作行
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                $("#saveDiv").removeClass("layui-hide");
                var index = layer.open({
                    title: '添加角色',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#saveDiv"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data;

                if(data.length > 0){
                    //js 函数  相当于  java 方法
                    layer.confirm("您确认删除选中的数据吗?!",function (index){

                        var arr = [];
                        for(let i = 0 ;i < data.length;i++){
                            arr[arr.length] = data[i].rid;
                        }


                        $.ajax({
                            method:"post",
                            contentType:"application/json;charset=utf-8",
                            datatype: "json",
                            data:JSON.stringify(arr),
                            url:"/rbac/role/dels",
                            success:function (d){
                               if(d){
                                   // 成功
                                   layer.msg("删除成功！！！",{icon:1,time:1000});
                                   //应该对当前页面里面的表格进行刷 新操作
                                   table.reload('mytable', {url: '/rbac/role/page'});
                               }else{
                                   // 失败
                                   layer.msg("删除失败！！！",{icon:2,time:1000});
                               }
                                layer.close(index);
                            }
                        });

                    });
                }else{
                    layer.msg("请选择您要删除的数据！？？",{icon:2,anim:6});
                }
            }
        });

        //这个是行操作栏
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $("#saveDiv").removeClass("layui-hide");
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#updateDiv"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

                //赋值
                form.val('updateform',{
                    "rid":data.rid,
                    "rname":data.rname,
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        method:"delete",  //请求方式 get post put delete
                        url:"/rbac/role/del/"+data.rid,// 请求地址  requesturl
                        success:function (d){
                            if(d){
                                // 成功
                                layer.msg("删除成功！！！",{icon:1,time:1000});
                                //应该对当前页面里面的表格进行刷 新操作
                                table.reload('mytable', {url: '/rbac/role/page'});
                            }else{
                                // 失败
                                layer.msg("删除失败！！！",{icon:2,time:1000});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'ice'){
                layer.confirm('确认冻结当前用户吗!?',function (index){
                    $.ajax({
                        method:"put",  //请求方式 get post put delete
                        url:"/rbac/role/updateIce/"+data.rid+"/"+(data.rstatus==0?1:0),// 请求地址  requesturl
                        success:function (d){
                            if(d){
                                // 成功
                                layer.msg("冻结成功！！！",{icon:1,time:1000});
                                //应该对当前页面里面的表格进行刷 新操作
                                table.reload('mytable', {url: '/rbac/role/page'});
                            }else{
                                // 失败
                                layer.msg("冻结成功！！！",{icon:2,time:1000});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === 'rolePermission'){
                //列出所有权限树
                $("#rolePermission").toggleClass("layui-hide");
                $("[name=rid]").val(data.rid);
                var index = layer.open({
                    title: '角色授权',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#rolePermission"),
                    cancel: function(index, layero){
                        $("#rolePermission").addClass("layui-hide");
                        return true;
                    }
                });

                $.ajax({
                    headers: {
                        Accept: "application/json; charset=utf-8"
                    }
                    ,contentType:"application/json; charset=utf-8"
                    ,method:'get'
                    ,async:false
                    ,url:'/rbac/permission/showPermissionTree'
                    ,data:{rid:data.rid}
                    ,success:function (d){
                        //可以重载所有基础参数
                        console.log(d);
                        tree.render({
                            elem: '#test7'
                            ,showCheckbox: true
                            ,data:d.allinfo
                            ,id:'pertree'
                        });
                    }
                });
                // 这个方法出现，jQuery连点事件 把当前点击事件的前一次操作 记录下来！ rid form.on()来完成操作
                //打勾所有当前角色有的权限
            }
        });

    });

</script>

</body>
</html>